<template>
  <view class="content">
    <template
      v-if="
        mydata.a.num > 0 ||
        mydata.b.num > 0 ||
        mydata.c.num > 0 ||
        mydata.d.num > 0 ||
        mydata.e.num > 0
      "
    >
      <view v-if="mydata.a.num > 0">
        <view>
          <image mode="aspectFill" src="/static/images/myl_logo.png"></image>
          <view class="title">商家置换沐浴露</view>
        </view>
        <view class="myl-num">
          <view>{{ mydata.a.num }}包</view>
          <view
            class="link-detail"
            @click="
              goto('/subpages/gift/flow-record?type=' + mydata.a.consume_type)
            "
            >置换详情</view
          >
        </view>
        <view class="btn">
          <button
            @click="
              goto('/pages/dog/myl/receiveDog?type=1&num=' + mydata.a.num)
            "
            type="default"
            size="mini"
          >
            去领养狗
          </button>
          <button @click="goto_no" type="default" size="mini">
            去置换消费
          </button>
          <button
            @click="
              goto('/pages/dog/myl/transferDog?type=1&num=' + mydata.a.num)
            "
            type="default"
            size="mini"
          >
            转给TA人
          </button>
          <button
            v-if="1 == mydata.a.is_buyback"
            @click="goto('/pages/dog/buyback/apply?type=1&num=' + mydata.a.num)"
            type="default"
            size="mini"
          >
            申请回购
          </button>
          <button @click="goto_no" type="default" size="mini">
            沐浴露提货
          </button>
        </view>
      </view>
      <view v-if="mydata.b.num > 0">
        <view>
          <image mode="aspectFill" src="/static/images/myl_logo.png"></image>
          <view class="title">养狗置换沐浴露</view>
        </view>
        <view class="myl-num">
          <view>{{ mydata.b.num }}包</view>
          <view
            class="link-detail"
            @click="
              goto('/subpages/gift/flow-record?type=' + mydata.b.consume_type)
            "
            >置换详情</view
          >
        </view>
        <view class="btn">
          <button
            @click="
              goto('/pages/dog/myl/receiveDog?type=2&num=' + mydata.b.num)
            "
            type="default"
            size="mini"
          >
            去领养狗
          </button>
          <button @click="goto_no" type="default" size="mini">
            去置换消费
          </button>
          <button
            v-if="1 == mydata.b.is_buyback"
            @click="goto('/pages/dog/buyback/apply?type=2&num=' + mydata.b.num)"
            type="default"
            size="mini"
          >
            申请回购
          </button>
          <!-- <button @click="goto('/pages/dog/myl/transferDog?type=2&num=' + mydata.b.num)" type="default" size="mini">转给TA人</button> -->
          <button @click="goto_no" type="default" size="mini">
            沐浴露提货
          </button>
        </view>
      </view>
      <view v-if="mydata.c.num > 0">
        <view>
          <image mode="aspectFill" src="/static/images/myl_logo.png"></image>
          <view class="title">分享奖励沐浴露</view>
        </view>
        <view class="myl-num">
          <view>{{ mydata.c.num }}包</view>
          <view
            class="link-detail"
            @click="
              goto('/subpages/gift/flow-record?type=' + mydata.c.consume_type)
            "
            >置换详情</view
          >
        </view>
        <view class="btn">
          <button
            @click="
              goto('/pages/dog/myl/receiveDog?type=3&num=' + mydata.c.num)
            "
            type="default"
            size="mini"
          >
            去领养狗
          </button>
          <button @click="goto_no" type="default" size="mini">
            去置换消费
          </button>
          <button
            v-if="1 == mydata.c.is_buyback"
            @click="goto('/pages/dog/buyback/apply?type=3&num=' + mydata.c.num)"
            type="default"
            size="mini"
          >
            申请回购
          </button>
          <!-- <button @click="goto('/pages/dog/myl/transferDog?type=3&num=' + mydata.c.num)" type="default" size="mini">转给TA人</button> -->
          <button @click="goto_no" type="default" size="mini">
            沐浴露提货
          </button>
        </view>
      </view>
      <view v-if="mydata.d.num > 0">
        <view>
          <image mode="aspectFill" src="/static/images/myl_logo.png"></image>
          <view class="title">新人赠送沐浴露</view>
        </view>
        <view class="myl-num">
          <view>{{ mydata.d.num }}包</view>
          <view
            class="link-detail"
            @click="
              goto('/subpages/gift/flow-record?type=' + mydata.d.consume_type)
            "
            >置换详情</view
          >
        </view>
        <view class="btn">
          <button
            @click="
              goto('/pages/dog/myl/receiveDog?type=4&num=' + mydata.d.num)
            "
            type="default"
            size="mini"
          >
            去领养狗
          </button>
          <button
            v-if="1 == mydata.d.is_buyback"
            @click="goto('/pages/dog/buyback/apply?type=4&num=' + mydata.d.num)"
            type="default"
            size="mini"
          >
            申请回购
          </button>
          <!-- <button @click="goto('/pages/mall/index/index')" type="default" size="mini">去置换消费</button> -->
          <!-- <button @click="goto('/pages/dog/myl/transferDog?type=4&num=' + mydata.d.num)" type="default" size="mini">转给TA人</button> -->
        </view>
      </view>
      <view v-if="mydata.e.num > 0">
        <view>
          <image mode="aspectFill" src="/static/images/myl_logo.png"></image>
          <view class="title">消费赠送沐浴露</view>
        </view>
        <view class="myl-num">
          <view>{{ mydata.e.num }}包</view>
          <view
            class="link-detail"
            @click="
              goto('/subpages/gift/flow-record?type=' + mydata.e.consume_type)
            "
            >置换详情</view
          >
        </view>

        <view class="btn">
          <button
            @click="
              goto('/pages/dog/myl/receiveDog?type=5&num=' + mydata.e.num)
            "
            type="default"
            size="mini"
          >
            去领养狗
          </button>
          <!-- <button @click="goto_no" type="default" size="mini">
            去置换消费
          </button> -->
          <button
            v-if="1 == mydata.e.is_buyback"
            @click="goto('/pages/dog/buyback/apply?type=5&num=' + mydata.e.num)"
            type="default"
            size="mini"
          >
            申请回购
          </button>
          <!-- <button @click="goto('/pages/dog/myl/transferDog?type=5&num=' + mydata.e.num)" type="default" size="mini">转给TA人</button> -->
          <button @click="goto_no" type="default" size="mini">
            沐浴露提货
          </button>
        </view>
      </view>
      <view v-if="mydata.f.num > 0">
        <view>
          <image mode="aspectFill" src="/static/images/myl_logo.png"></image>
          <view class="title">商家增值沐浴露</view>
        </view>
        <view class="myl-num">
          <view>{{ mydata.f.num }}包</view>
          <view
            class="link-detail"
            @click="
              goto('/subpages/gift/flow-record?type=' + mydata.f.consume_type)
            "
            >置换详情</view
          >
        </view>
        <view class="btn">
          <button
            @click="
              goto('/pages/dog/myl/receiveDog?type=6&num=' + mydata.f.num)
            "
            type="default"
            size="mini"
          >
            去领养狗
          </button>
        </view>
      </view>
      <view v-if="mydata.g.num > 0">
        <view>
          <image mode="aspectFill" src="/static/images/myl_logo.png"></image>
          <view class="title">商家促销沐浴露</view>
        </view>
        <view class="myl-num">
          <view>{{ mydata.g.num }}包</view>
          <view
            class="link-detail"
            @click="
              goto('/subpages/gift/flow-record?type=' + mydata.g.consume_type)
            "
            >置换详情</view
          >
        </view>

        <view class="btn">
          <button
            @click="
              goto(
                '/pages/dog/myl/transferDog?type=' +
                  mydata.g.consume_type +
                  '&num=' +
                  mydata.g.num
              )
            "
            type="default"
            size="mini"
          >
            转给TA人
          </button>
          <button
            @click="goto('/subpages/gift/index')"
            type="default"
            size="mini"
          >
            发放礼卡
          </button>
        </view>
      </view>
    </template>
    <statusTips v-else text="暂无沐浴露"></statusTips>
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { getUserCoin } from "@/api/bgame";
import { onLoad, onShow } from "@dcloudio/uni-app";
import statusTips from "@/components/statusTips/statusTips.vue";
import { useUserStoreHook } from "@/store/user";
const mydataInit = {
  1: "a",
  2: "b",
  3: "c",
  4: "d",
  5: "e",
  6: "f",
  7: "g",
};
const mydata = ref({
  a: { num: 0 },
  b: { num: 0 },
  c: { num: 0 },
  d: { num: 0 },
  e: { num: 0 },
  f: { num: 0 },
  g: { num: 0 },
});

const mallconfig = ref(0);

const getList = async () => {
  Object.assign(mydata.value, {
    a: { num: 0 },
    b: { num: 0 },
    c: { num: 0 },
    d: { num: 0 },
    e: { num: 0 },
    f: { num: 0 },
    g: { num: 0 },
  });
  uni.showLoading({
    title: "获取数据中...",
    mask: true,
  });
  const res = await getUserCoin({});
  console.log(res, "aaaaaaa");
  uni.hideLoading();
  if (1 == res.result) {
    let list = res.data || [];
    list.forEach((item) => {
      let obj = {};
      obj[mydataInit[item.consume_type]] = {
        num: Number(item.num || 0),
        consume_type: item.consume_type,
        is_buyback: item.is_buyback,
      };
      Object.assign(mydata.value, obj);
    });
  }
};
const goto = (path) => {
  uni.navigateTo({
    url: path,
  });
};
const goto_no = () => {
  uni.showToast({
    title: "开发中,尽请期待。。。",
    icon: "none",
  });
};
onShow(async () => {
  mallconfig.value = uni.getStorageSync('mallconfig') || 0;

  // 检查是否已绑定手机号
  if (mallconfig.value === 1) {
    const phone = uni.getStorageSync('phone');
    if (!phone) {
      uni.showModal({
        title: '提示',
        content: '访问此页面需要先绑定手机号',
        confirmText: '去绑定',
        cancelText: '返回',
        success: (res) => {
          if (res.confirm) {
            uni.navigateTo({
              url: '/pages/login/bind-mobile'
            });
          } else {
            uni.navigateBack();
          }
        }
      });
      return;
    }
  }

  await useUserStoreHook().getSign();
  getList();
});
</script>

<style lang="scss" scoped>
.content {
  width: 100%;
  height: calc(100vh - 44px);
  padding: 8rpx;

  .title {
    font-size: 0.8rem;
  }

  & > view {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 10rpx;
    border: green 1px solid;
    border-radius: 5rpx;
    padding-left: 50rpx;

    view {
      padding: 5rpx;

      image {
        width: 20vw;
        height: 14vh;
      }
    }

    .btn {
      display: flex;
      flex-direction: column;
      margin-right: 100rpx;

      button {
        margin-top: 4rpx;
        width: 200rpx;
        border: green 1px solid;
        color: green;
        font-size: 24rpx;
      }
    }
  }
}
.link-detail {
  color: #00a971;
  font-size: 26rpx;
  margin-top: 8rpx;
  text-decoration: underline;
  cursor: pointer;
}
</style>
